package com.qxzn.app.medicalhealth.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.View;

import com.github.jdsjlzx.interfaces.OnItemClickListener;
import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.qxzn.app.api.api.HttpRequest;
import com.qxzn.app.bean.RobotDeviceListBean;
import com.qxzn.app.medicalhealth.R;
import com.qxzn.app.medicalhealth.R2;
import com.qxzn.app.medicalhealth.adapter.AddedMedicalDeviceAdapter;
import com.qxzn.app.page.BaseActivity;
import com.qxzn.app.page.activity.QrScanActivity;
import com.qxzn.app.util.AppUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 已添加的医疗设备列表
 */
public class AddedMedicalDeviceActivity extends BaseActivity {

    @BindView(R2.id.lrv_robot_list)
    LRecyclerView lrvRobotList;
    private LRecyclerViewAdapter recyclerViewAdapter;
    private List<RobotDeviceListBean.DataBean> dataList = new ArrayList();
    private int page = 1;
    private int pageSize = 10;
    private AddedMedicalDeviceAdapter adapter;
    private Handler handler = new Handler();

    @Override
    protected int initLayout() {
        return R.layout.activity_added_medical_device;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void iniTitleBar() {
        tv_title.setText("已添加的医疗设备");
        iv_right.setVisibility(View.VISIBLE);
        iv_right.setImageResource(R.mipmap.logo);
        iv_right.setOnClickListener(this);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        lrvRobotList.setLayoutManager(new LinearLayoutManager(this));
        adapter = new AddedMedicalDeviceAdapter(this, dataList);
        recyclerViewAdapter = new LRecyclerViewAdapter(adapter);
        lrvRobotList.setAdapter(recyclerViewAdapter);
        recyclerViewAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                AppUtil.showLog("点击位置：" + position);
            }
        });
        lrvRobotList.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                AppUtil.showLog("setOnRefreshListener");
                page = 1;
                getData();
            }
        });
        lrvRobotList.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                AppUtil.showLog("setOnLoadMoreListener");
                page++;
                getData();
            }
        });
        getData();
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        if (view.getId() == R.id.iv_right) {
            startActivity(MedicalDeviceActivity.class);
        }
    }

    private void getData() {
//        handler.postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                lrvRobotList.refreshComplete(pageSize);
//            }
//        }, 1000);
        for (int i = (page - 1) * pageSize; i < page * pageSize; i++) {
            dataList.add(new RobotDeviceListBean.DataBean());
        }
        recyclerViewAdapter.notifyDataSetChanged();
        recyclerViewAdapter.notifyItemRangeChanged((page - 1) * pageSize, pageSize);

        Call<RobotDeviceListBean> call = HttpRequest.getService().getRobotDeviceList();
        call.enqueue(new Callback<RobotDeviceListBean>() {
            @Override
            public void onResponse(Call<RobotDeviceListBean> call, Response<RobotDeviceListBean> response) {
                lrvRobotList.refreshComplete(pageSize);
                RobotDeviceListBean keyBean = response.body();
                if (keyBean != null) {
                    List<RobotDeviceListBean.DataBean> deviceList = keyBean.getData();
                    if (deviceList != null) {
                        if (page == 1) {
                            dataList.clear();
                        }
                        dataList.addAll(deviceList);
                        recyclerViewAdapter.notifyDataSetChanged();
                        return;
                    }
                }
                if (keyBean != null && !TextUtils.isEmpty(keyBean.getMsg())) {
                    AppUtil.showToast(AddedMedicalDeviceActivity.this, "网络错误，请稍后再试！");
                } else {
                    AppUtil.showToast(AddedMedicalDeviceActivity.this, keyBean.getMsg());
                }
//                toAddDevice();
//                finish();
            }

            @Override
            public void onFailure(Call<RobotDeviceListBean> call, Throwable t) {
                AppUtil.showToast(AddedMedicalDeviceActivity.this, "网络错误，请稍后再试！");
//                finish();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacksAndMessages(null);
    }
}
